<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--景区查询条件数据-->
      <el-col v-if="isSuperAdmin" :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="scenicName"
            placeholder="请输入景区名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="scenicOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>

      <!--码头数据-->
      <el-col :span="isSuperAdmin?20:24" :xs="24">
        <!--搜索条件输入表单-->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px" style="margin-bottom: 15px">
          <el-form-item label="码头编号" prop="wharfCode" label-width="80px">
            <el-input
              v-model="queryParams.wharfCode"
              placeholder="请输入码头的字母序号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="码头名称" prop="wharfName" label-width="80px">
            <el-input
              v-model="queryParams.wharfName"
              placeholder="请输入码头的名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label-width="120px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!--操作按钮区-->
        <el-row :gutter="10" class="mb8">
          <el-col v-if="isSuperAdmin" :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:wharf:add']"
            >新增
            </el-button>
          </el-col>
          <el-col v-if="isSuperAdmin" :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:wharf:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col v-if="isSuperAdmin" :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:wharf:remove']"
            >删除
            </el-button>
          </el-col>
          <!--          <el-col :span="1.5">-->
          <!--            <el-button-->
          <!--              type="warning"-->
          <!--              plain-->
          <!--              icon="el-icon-download"-->
          <!--              size="mini"-->
          <!--              @click="handleExport"-->
          <!--              v-hasPermi="['system:wharf:export']"-->
          <!--            >导出-->
          <!--            </el-button>-->
          <!--          </el-col>-->
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getWharfList"></right-toolbar>
        </el-row>

        <!--搜索结果列表区-->
        <el-table v-loading="loading" :data="wharfList" @selection-change="handleSelectionChange" style="width: 100%">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="码头ID" align="center" key="wharfId" prop="wharfId"/>
          <el-table-column label="码头编号" align="center" key="wharfCode" prop="wharfCode"/>
          <el-table-column label="码头名称" align="center" key="wharfName" prop="wharfName"/>
          <el-table-column label="所属景区" align="center" key="scenicName" prop="scenic.scenicName" width="240"/>
          <el-table-column label="启用状态" align="center" key="status" v-if="!isSuperAdmin" width="120">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="经度" align="center" key="lon" prop="lon"/>
          <el-table-column label="纬度" align="center" key="lat" prop="lat"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="isSuperAdmin">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:wharf:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:wharf:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getWharfList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改景区码头对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="left">
        <el-form-item label="所属景区" prop="scenicId" label-width="80px">
          <treeselect v-model="form.scenicId" :options="scenicOptions" :show-count="true"
                      placeholder="请选择归属景区"/>
        </el-form-item>
        <el-form-item label="码头编号" prop="wharfCode" label-width="80px">
          <el-input v-model="form.wharfCode" placeholder="请输入码头的编号(示例:A,B,...)"/>
        </el-form-item>
        <el-form-item label="码头名称" prop="wharfName" label-width="80px">
          <el-input v-model="form.wharfName" placeholder="请输入码头的名称"/>
        </el-form-item>
        <el-form-item label="经 度" prop="lon">
          <el-input v-model="form.lon"
                    placeholder="请输入码头位置坐标经度"/>
        </el-form-item>
        <el-form-item label="纬 度" prop="lat">
          <el-input v-model="form.lat"
                    placeholder="请输入码头位置坐标纬度"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listWharf, getWharf, delWharf, addWharf, updateWharf, changeWharfStatus} from "@/api/system/wharf";
import {listScenic} from '@/api/system/scenic';
import {changeUserStatus, scenicTreeSelect} from "@/api/system/user";
import auth from "@/plugins/auth";
import store from "@/store";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Wharf",
  components: {Treeselect},
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 景区码头，每个景区可能有1个或多个码头，景区与码头之间属于一对多的关系。表格数据
      wharfList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wharfCode: '',
        wharfName: '',
        lon: undefined,
        lat: undefined,
        scenicId: undefined,
        status: undefined
      },
      // 表单参数
      form: {
        wharfId: undefined,
        wharfCode: '',
        wharfName: '',
        lon: undefined,
        lat: undefined,
        scenicId: undefined,
        status: "0",
      },
      // 表单校验
      rules: {
        scenicId: [
          {required: true, message: "所属景区不能为空", trigger: "blur"},
        ],
        wharfCode: [
          {required: true, message: "码头编号不能为空", trigger: "blur"},
          {min: 1, max: 1, message: '码头编号只能为一位大写英文字母', trigger: 'blur'}
        ],
        wharfName: [
          {required: true, message: "码头名称不能为空", trigger: "blur"},
        ],
        lon: [
          {required: true, message: "码头坐标经度不能为空", trigger: "blur"},
        ],
        lat: [
          {required: true, message: "码头坐标纬度不能为空", trigger: "blur"},
        ],
      },
      scenicName: '',
      // 景区树选项
      scenicOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      isSuperAdmin: false
    };
  },
  created() {
    this.isSuperAdmin = auth.isSuperAdmin();
    if (this.isSuperAdmin) {
      /** 查询景区下拉树结构 */
      this.getScenicTree();
    }
    // else {
    //   this.queryParams.scenicId = store.getters.scenicId;
    // }
    //查询所有码头列表
    this.getWharfList();
  },
  methods: {
    /** 查询景区码头列表，每个景区可能有1个或多个码头，景区与码头之间属于一对多的关系。列表 */
    getWharfList() {
      this.loading = true;
      listWharf(this.queryParams).then(response => {
        this.wharfList = response.rows;
        console.log("this.wharfList: ", this.wharfList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        wharfId: undefined,
        wharfCode: undefined,
        wharfName: undefined,
        lon: undefined,
        lat: undefined,
        scenicId: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getWharfList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wharfId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加码头";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const wharfId = row.wharfId || this.ids
      getWharf(wharfId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改码头信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.wharfId != null) {
            updateWharf(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getWharfList();
            });
          } else {
            console.log("submitForm form: ", this.form)
            addWharf(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getWharfList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wharfIds = row.wharfId || this.ids;
      this.$modal.confirm('是否确认删除码头"' + wharfIds + '"的数据项？').then(function () {
        return delWharf(wharfIds);
      }).then(() => {
        this.getWharfList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/wharf/export', {
        ...this.queryParams
      }, `wharf_${new Date().getTime()}.xlsx`)
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      // this.queryParams.deptId = data.id;
      this.queryParams.scenicId = data.id;
      this.handleQuery();
    },
    /** 查询景区下拉树结构 */
    getScenicTree() {
      scenicTreeSelect().then(response => {
        this.scenicOptions = response.data;
      });
    },
    // 码头状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm("确认要" + text + '"' + row.wharfName + '"' + "吗？").then(function () {
        return changeWharfStatus(row.wharfId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
  }
};
</script>

<style>
.el-form-item__content button:nth-child(1) {
  margin-left: 15px;
}
</style>
